<?php

namespace app\forms;

use app\models\Group;
use app\components\validators\Exist;
use app\components\validators\PositiveInteger;

/**
 * 组织表单类
 */
class GroupForm extends Model
{
    public $id;
    public $parentId;
    public $name;
    public $sort;

    public function rules()
    {
        return [
            ['id', PositiveInteger::class],

            ['parentId', 'integer'],
            ['parentId', 'default', 'value' => 0],
            ['parentId', Exist::class, 'targetClass' => Group::class, 'targetAttribute' => 'id'],

            ['name', 'trim'],
            ['name', 'string', 'max' => 32],

            ['sort', 'integer'],
            ['sort', 'default', 'value' => 0],

            ['id', 'required', 'on' => ['update', 'get-info', 'delete']],
            ['id', 'exist', 'targetClass' => Group::class, 'on' => ['update', 'get-info', 'delete']],

            ['name', 'required', 'on' => ['create', 'update']],
            ['name', 'unique', 'targetClass' => Group::class, 'on' => ['create']],
            ['name', 'unique', 'targetClass' => Group::class, 'filter' => ['<>', 'id', $this->id], 'on' => ['update']],
        ];
    }

    public function scenarios()
    {
        return [
            'create' => ['parentId', 'name', 'sort'],
            'update' => ['id', 'name', 'sort'],
            'delete' => ['id'],
            'get-info' => ['id']
        ];
    }

}